package com.wg.admin.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.wg.common.annotation.Log;
import com.wg.common.annotation.RepeatSubmit;
import com.wg.common.core.controller.BaseController;
import com.wg.common.core.domain.PageQuery;
import com.wg.common.core.domain.R;
import com.wg.common.core.domain.TableDataInfo;
import com.wg.common.core.validate.AddGroup;
import com.wg.common.core.validate.EditGroup;
import com.wg.common.enums.BusinessType;
import com.wg.system.entity.SysFileConfig;
import com.wg.system.service.SysFileConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * 对象存储配置表(SysFileConfig)控制层
 *
 * @author wg
 * @date 2022-05-06 11:18:31
 */
@Validated
@Api(value = "对象存储配置控制器", tags = {"对象存储配置管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/oss/config")
public class SysFileConfigController extends BaseController {

    private final SysFileConfigService sysFileConfigService;

    /**
     * 查询对象存储配置列表
     */
    @ApiOperation("查询对象存储配置列表")
    @SaCheckPermission("system:oss:list")
    @GetMapping("/list")
    public TableDataInfo<SysFileConfig> list(SysFileConfig fileConfig, PageQuery pageQuery) {
        return sysFileConfigService.queryPageList(fileConfig, pageQuery);
    }

    /**
     * 获取对象存储配置详细信息
     */
    @ApiOperation("获取对象存储配置详细信息")
    @SaCheckPermission("system:oss:query")
    @GetMapping("/{id}")
    public R<SysFileConfig> getInfo(@ApiParam("file配置id")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("id") Integer id) {
        return R.ok(sysFileConfigService.getById(id));
    }

    /**
     * 新增对象存储配置
     */
    @ApiOperation("新增对象存储配置")
    @SaCheckPermission("system:oss:add")
    @Log(title = "对象存储配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysFileConfig fileConfig) {
        return toAjax(sysFileConfigService.insert(fileConfig) ? 1 : 0);
    }

    /**
     * 修改对象存储配置
     */
    @ApiOperation("修改对象存储配置")
    @SaCheckPermission("system:oss:edit")
    @Log(title = "对象存储配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysFileConfig fileConfig) {
        return toAjax(sysFileConfigService.update(fileConfig) ? 1 : 0);
    }

    /**
     * 删除对象存储配置
     */
    @ApiOperation("删除对象存储配置")
    @SaCheckPermission("system:oss:remove")
    @Log(title = "对象存储配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ossConfigIds}")
    public R<Void> remove(@ApiParam("OSS配置ID串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ossConfigIds) {
        return toAjax(sysFileConfigService.deleteWithValidByIds(Arrays.asList(ossConfigIds), true) ? 1 : 0);
    }

    /**
     * 状态修改
     */
    @ApiOperation("状态修改")
    @SaCheckPermission("system:oss:edit")
    @Log(title = "对象存储状态修改", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public R<Void> changeStatus(@RequestBody SysFileConfig fileConfig) {
        return toAjax(sysFileConfigService.updateFileConfigStatus(fileConfig));
    }



}


